<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="寄存器名" prop="tagname">
        <el-input
          v-model="queryParams.tagname"
          placeholder="请输入寄存器名称"
          clearable
          @keyup.enter.native="handleQuery"
          id="tagname"
        />
      </el-form-item>
      <el-form-item label="中文含义" prop="tagnameCN">
        <el-input
          v-model="queryParams.tagnameCN"
          placeholder="请输入寄存器的中文含义"
          clearable
          @keyup.enter.native="handleQuery"
          id="tagnameCN"
        />
      </el-form-item>
      <el-form-item label="地址" prop="itemid">
        <el-input
          v-model="queryParams.itemid"
          placeholder="请输入寄存器地址"
          clearable
          @keyup.enter.native="handleQuery"
          id="itemid"
        />
      </el-form-item>
      <el-form-item label="单位" prop="units">
        <el-input
          v-model="queryParams.units"
          placeholder="请输入单位"
          clearable
          @keyup.enter.native="handleQuery"
          id="units"
        />
      </el-form-item>
      <el-form-item label="值类型" prop="valuetype">
        <el-select v-model="queryParams.valuetype" placeholder="请选择值类型" id="valuetype" @change="handleQuery" clearable>
          <el-option
            v-for="dict in dict.type.sys_value_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="采集频率" prop="savetime">
        <el-select v-model="queryParams.savetime" placeholder="请选择变量采集频率" id="savetime" @change="handleQuery" clearable>
          <el-option
            v-for="dict in dict.type.sys_data_save_time"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="读写属性" prop="itemreadtype">
        <el-select v-model="queryParams.itemreadtype" placeholder="请选择读写属性" id="itemreadtype" @change="handleQuery" clearable>
          <el-option
            v-for="dict in dict.type.sys_read_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="权限" prop="control">
        <el-select v-model="queryParams.control" placeholder="请选择控制权限" id="control" @change="handleQuery" clearable>
          <el-option
            v-for="dict in dict.type.sys_control_permissions"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="系数" prop="itemxs">
        <el-input
          v-model="queryParams.itemxs"
          placeholder="请输入系数"
          clearable
          @keyup.enter.native="handleQuery"
          id="itemxs"
        />
      </el-form-item>
      <el-form-item label="是否报警" prop="alarmtag">
        <el-select v-model="queryParams.alarmtag" placeholder="请选择是否报警" id="alarmtag" @change="handleQuery" clearable>
          <el-option
            v-for="dict in dict.type.sys_is_alarm"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
<!--      <el-form-item label="禁止报警" prop="alarmforbid">
        <el-select v-model="queryParams.alarmforbid" placeholder="请选择是否禁止报警" @change="handleQuery" clearable>
          <el-option
            v-for="dict in dict.type.sys_alarm_forbid"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>-->
      <el-form-item label="报警分组" prop="alarmgroup">
        <el-input
          v-model="queryParams.alarmgroup"
          placeholder="请输入报警分组"
          clearable
          @keyup.enter.native="handleQuery"
          id="alarmgroup"
        />
      </el-form-item>
<!--      <el-form-item label="报警延时" prop="alarmdelay">
        <el-input
          v-model="queryParams.alarmdelay"
          placeholder="请输入报警延时"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>-->
      <el-form-item label="高高限值" prop="hhValue">
        <el-input
          v-model="queryParams.hhValue"
          placeholder="请输入高高限值"
          clearable
          @keyup.enter.native="handleQuery"
          id="hhValue"
        />
      </el-form-item>
      <el-form-item label="高限值" prop="hvalue">
        <el-input
          v-model="queryParams.hvalue"
          placeholder="请输入高限值"
          clearable
          @keyup.enter.native="handleQuery"
          id="hvalue"
        />
      </el-form-item>
      <el-form-item label="低限" prop="lvalue">
        <el-input
          v-model="queryParams.lvalue"
          placeholder="请输入低限"
          clearable
          @keyup.enter.native="handleQuery"
          id="lvalue"
        />
      </el-form-item>
      <el-form-item label="低低限" prop="llValue">
        <el-input
          v-model="queryParams.llValue"
          placeholder="请输入低低限"
          clearable
          @keyup.enter.native="handleQuery"
          id="llValue"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:qstag:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:qstag:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:qstag:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="info"
          icon="el-icon-upload2"
          size="mini"
          @click="handleImport"
          v-hasPermi="['system:qstag:import']"
        >导入</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:qstag:export']"
        >导出</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:qstag:export']"
        >导出为新项目的PLC点表（待完成）</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList" :columns="columns"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="qstagList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="ID" align="center" prop="tagid" v-if="columns[0].visible" width="60"/>
      <el-table-column label="寄存器名称" align="center" prop="tagname" v-if="columns[1].visible" width="280" :show-overflow-tooltip="true"/>
      <el-table-column label="寄存器中文含义" align="center" prop="tagnameCN" v-if="columns[2].visible" width="170" :show-overflow-tooltip="true"/>
      <el-table-column label="寄存器地址" align="center" prop="itemid" v-if="columns[3].visible"  width="110" :show-overflow-tooltip="true"/>
      <el-table-column label="寄存器值" align="center" prop="tagvalue" v-if="columns[4].visible" />
      <el-table-column label="单位" align="center" prop="units" v-if="columns[5].visible" />
<!--      <el-table-column label="描述" align="center" prop="tagdesc" v-if="columns[6].visible" />-->
      <el-table-column label="值类型" align="center" prop="valuetype" v-if="columns[7].visible" width="130" :show-overflow-tooltip="true">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_value_type" :value="scope.row.valuetype"/>
        </template>
      </el-table-column>
      <el-table-column label="网关接口名称" align="center" prop="itemDrIdName" v-if="columns[8].visible" :show-overflow-tooltip="true"/>
      <el-table-column label="采集频率" align="center" prop="savetime" v-if="columns[9].visible" width="80" :show-overflow-tooltip="true">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_data_save_time" :value="scope.row.savetime"/>
        </template>
      </el-table-column>
      <el-table-column label="读写" align="center" prop="itemreadtype" v-if="columns[10].visible" width="60">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_read_type" :value="scope.row.itemreadtype"/>
        </template>
      </el-table-column>
      <el-table-column label="权限" align="center" prop="control" v-if="columns[11].visible" width="70">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_control_permissions" :value="scope.row.control"/>
        </template>
      </el-table-column>
      <el-table-column label="系数" align="center" prop="itemxs" v-if="columns[12].visible" width="50"/>
      <el-table-column label="更新时间" align="center" prop="updatetime" v-if="columns[13].visible" width="150" :show-overflow-tooltip="true"/>
      <el-table-column label="是否报警" align="center" prop="alarmtag" v-if="columns[14].visible">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_is_alarm" :value="scope.row.alarmtag"/>
        </template>
      </el-table-column>
      <el-table-column label="是否禁止" align="center" prop="alarmforbid" v-if="columns[15].visible">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_alarm_forbid" :value="scope.row.alarmforbid"/>
        </template>
      </el-table-column>
      <el-table-column label="报警分组" align="center" prop="alarmgroup" v-if="columns[16].visible" :show-overflow-tooltip="true"/>
      <el-table-column label="报警等级" align="center" prop="alarmtypeExpain" v-if="columns[17].visible" :show-overflow-tooltip="true"/>
      <el-table-column label="报警延时" align="center" prop="alarmdelay" v-if="columns[18].visible" :show-overflow-tooltip="true"/>
      <el-table-column label="高高限值" align="center" prop="hhValue" v-if="columns[19].visible" />
      <el-table-column label="高高限报警等级" align="center" prop="hhAlarmTypeExplain" v-if="columns[23].visible" :show-overflow-tooltip="true"/>
      <el-table-column label="高限值" align="center" prop="hvalue" v-if="columns[20].visible" />
      <el-table-column label="高限报警等级" align="center" prop="halarmTypeExplain" v-if="columns[24].visible" :show-overflow-tooltip="true"/>
      <el-table-column label="低限值" align="center" prop="lvalue" v-if="columns[21].visible" />
      <el-table-column label="低限报警等级" align="center" prop="lalarmTypeExplain" v-if="columns[25].visible" :show-overflow-tooltip="true"/>
      <el-table-column label="低低限值" align="center" prop="llValue" v-if="columns[22].visible" />
      <el-table-column label="低低限报警等级" align="center" prop="llAlarmTypeExplain" v-if="columns[26].visible" :show-overflow-tooltip="true"/>
      <el-table-column label="操作" align="center" class-name="small-padding" width="150">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleFormula(scope.row)"
            v-hasPermi="['system:qstag:edit']"
          >公式</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:qstag:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:qstag:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 导入对话框 -->
    <el-dialog :title="upload.title" :visible.sync="upload.open" width="400px" append-to-body>
      <el-upload
        ref="upload"
        :limit="1"
        accept=".xlsx, .xls"
        :headers="upload.headers"
        :action="upload.url + '?updateSupport=' + upload.updateSupport + '&dbName=' + upload.dbName"
        :disabled="upload.isUploading"
        :on-progress="handleFileUploadProgress"
        :on-success="handleFileSuccess"
        :auto-upload="false"
        drag
      >
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
        <div class="el-upload__tip text-center" slot="tip">
          <div class="el-upload__tip" slot="tip">
            <el-checkbox v-model="upload.updateSupport" /> 是否更新已经存在的项目点表配置数据
          </div>
          <span>仅允许导入xls、xlsx格式文件。</span>
          <el-link type="primary" :underline="false" style="font-size:12px;vertical-align: baseline;" @click="importTemplate">下载模板</el-link>
        </div>
      </el-upload>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFileForm">确 定</el-button>
        <el-button @click="upload.open = false">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 添加或修改变量寄存器配置对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1000px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="寄存器名" prop="tagname">
              <el-input v-model="form.tagname" placeholder="英文名称，如“CH1-502-40001:8”，或“CHP1-506-40001”" :disabled="form.itemid === 'local'"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="中文含义" prop="tagnameCN">
              <el-input v-model="form.tagnameCN" placeholder="中文名称，如“CH1远程”，或“CHP1转速”" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="寄存地址" prop="itemid">
              <el-input v-model="form.itemid" placeholder="ModbusTCP寄存器地址，如“502-40001:8”，或“506-40001”" :disabled="form.itemid === 'local'"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="类型代码" prop="drTypeCode">
              <el-input v-model="form.drTypeCode" placeholder="请输入所属设备类型代码，如“CH”，“CHP”，“CWP”" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="从站地址" prop="itemdradd">
              <el-input v-model="form.itemdradd" placeholder="请输入Modbus Tcp网关的从站地址，如1，2，3" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="值类型" prop="valuetype">
              <el-select v-model="form.valuetype" placeholder="请选择值类型">
                <el-option
                  v-for="dict in dict.type.sys_value_type"
                  :key="dict.value"
                  :label="dict.label"
                  :value="parseInt(dict.value)"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="系数" prop="itemxs">
              <el-input v-model="form.itemxs" placeholder="请输入系数，如1.5，0.05等" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="初始值" prop="inivalue">
              <el-input v-model="form.inivalue" placeholder="请输入点位初始值，如0，1，50.5，100.3" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="控制权限" prop="control">
              <el-select v-model="form.control" placeholder="请选择控制权限">
                <el-option
                  v-for="dict in dict.type.sys_control_permissions"
                  :key="dict.value"
                  :label="dict.label"
                  :value="parseInt(dict.value)"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="当前值" prop="tagvalue">
              <el-input v-model="form.tagvalue" placeholder="无需输入" readonly/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="读写属性" prop="itemreadtype">
              <el-select v-model="form.itemreadtype" placeholder="请选择点位的读写属性">
                <el-option
                  v-for="dict in dict.type.sys_read_type"
                  :key="dict.value"
                  :label="dict.label"
                  :value="parseInt(dict.value)"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="单位" prop="units">
              <el-input v-model="form.units" placeholder="请输入单位，如°C，A，V，HZ" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="采集频率" prop="savetime">
              <el-select v-model="form.savetime" placeholder="存储为历史数据的时间间隔">
                <el-option
                  v-for="dict in dict.type.sys_data_save_time"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="变量描述" prop="tagdesc">
              <el-input v-model="form.tagdesc" placeholder="请输入寄存器的描述内容，如“1远程；0本地”" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="能量计量" prop="meteringGroup">
              <el-input v-model="form.meteringGroup" placeholder="“设备编号_名称_点位编号”，如：ey1_车间中温冷冻_T2" />
            </el-form-item>
          </el-col>
        </el-row>
<!--        <el-row>
          <el-col :span="12">
            <el-form-item label="是否报警" prop="alarmtag">
              <el-select v-model="form.alarmtag" placeholder="是否报警(仅布尔变量)">
                <el-option
                  v-for="dict in dict.type.sys_is_alarm"
                  :key="dict.value"
                  :label="dict.label"
                  :value="parseInt(dict.value)"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="是否禁止" prop="alarmforbid">
              <el-select v-model="form.alarmforbid" placeholder="是否禁止报警(仅布尔变量)">
                <el-option
                  v-for="dict in dict.type.sys_alarm_forbid"
                  :key="dict.value"
                  :label="dict.label"
                  :value="parseInt(dict.value)"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>-->
        <el-row>
          <el-col :span="12">
            <el-form-item label="布尔变量" prop="boolType">
              <el-select v-model="form.boolType" placeholder="请选择是否布尔变量">
                <el-option
                  v-for="dict in dict.type.sys_is_custom_type"
                  :key="dict.value"
                  :label="dict.label"
                  :value="parseInt(dict.value)"
                ></el-option>
              </el-select>(是否当布尔变量值来处理)
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="报警等级" prop="alarmlevel" style="padding-left: 12px" label-width="90">
              <el-select v-model="form.alarmlevel" placeholder="报警等级(仅布尔变量)" clearable :disabled="form.boolType === 0">
                <el-option
                  v-for="alarmLevel in alarmLevelOptions"
                  :key="alarmLevel.alarmtypelevel"
                  :label="alarmLevel.alarmtypeExpain"
                  :value="parseInt(alarmLevel.alarmtypelevel)"
                ></el-option>
              </el-select>(仅布尔变量的值类型)
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="报警备注" prop="alarmnote">
              <el-input v-model="form.alarmnote" placeholder="请输入报警备注(全部值类型通用)" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="报警延时(单位：秒)" prop="alarmdelay" style="padding-left: 12px" label-width="90">
              <el-input v-model="form.alarmdelay" placeholder="请输入报警延时(全部值类型通用)，单位：秒" style="width: 332px"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="高高限值" prop="hhValue">
              <el-input v-model="form.hhValue" placeholder="请输入高高限值(非布尔值的其它值类型)" :disabled="form.boolType === 1"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="高高限报警等级" prop="hhAlarmLevel" style="padding-left: 12px" label-width="90">
              <el-select v-model="form.hhAlarmLevel" placeholder="请选择高高限值报警等级" clearable :disabled="form.boolType === 1">
                <el-option
                  v-for="alarmLevel in alarmLevelOptions"
                  :key="alarmLevel.alarmtypelevel"
                  :label="alarmLevel.alarmtypeExpain"
                  :value="parseInt(alarmLevel.alarmtypelevel)"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="高限值" prop="hvalue">
              <el-input v-model="form.hvalue" placeholder="请输入高限值(非布尔值的其它值类型)" :disabled="form.boolType === 1"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="高限报警等级" prop="halarmLevel" style="padding-left: 12px" label-width="90">
              <el-select v-model="form.halarmLevel" placeholder="请选择高限报警等级" clearable :disabled="form.boolType === 1">
                <el-option
                  v-for="dict in alarmLevelOptions"
                  :key="dict.alarmtypelevel"
                  :label="dict.alarmtypeExpain"
                  :value="parseInt(dict.alarmtypelevel)"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="低限值" prop="lvalue">
              <el-input v-model="form.lvalue" placeholder="请输入低限值(非布尔值的其它值类型)" :disabled="form.boolType === 1"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="低限报警等级" prop="lalarmLevel" style="padding-left: 12px" label-width="90">
              <el-select v-model="form.lalarmLevel" placeholder="请选择低限报警等级" clearable :disabled="form.boolType === 1">
                <el-option
                  v-for="dict in alarmLevelOptions"
                  :key="dict.alarmtypelevel"
                  :label="dict.alarmtypeExpain"
                  :value="parseInt(dict.alarmtypelevel)"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="低低限值" prop="llValue">
              <el-input v-model="form.llValue" placeholder="请输入低低限值(非布尔值的其它值类型)" :disabled="form.boolType === 1"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="低低限报警等级" prop="llAlarmLevel" style="padding-left: 12px" label-width="90">
              <el-select v-model="form.llAlarmLevel" placeholder="请选择低低限报警等级" clearable :disabled="form.boolType === 1">
                <el-option
                  v-for="dict in alarmLevelOptions"
                  :key="dict.alarmtypelevel"
                  :label="dict.alarmtypeExpain"
                  :value="parseInt(dict.alarmtypelevel)"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
<!--        <el-row>
          <el-col :span="12">
            <el-form-item label="计算公式" prop="tagformula">
              <el-input v-model="form.tagformula" type="textarea" placeholder="请输入内容" />
            </el-form-item>
          </el-col>
          <el-col :span="12">

          </el-col>
        </el-row>-->

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 编辑变量寄存器公式对话框 -->
    <el-dialog :title="formulaTitle" :visible.sync="formulaOpen" width="1000px" append-to-body>
      <el-form ref="formulaForm" :model="formulaForm" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="6">
            <el-form-item label="寄存器名" prop="tagname">
              <el-input v-model="formulaForm.tagname" placeholder="英文名称，如“CH1-502-40001:8”，或“CHP1-506-40001”" disabled/>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="中文含义" prop="tagnameCN">
              <el-input v-model="formulaForm.tagnameCN" placeholder="中文名称，如“CH1远程”，或“CHP1转速”" disabled/>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="寄存地址" prop="itemid">
              <el-input v-model="formulaForm.itemid" placeholder="ModbusTCP寄存器地址，如“502-40001:8”，或“506-40001”" disabled/>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="值类型" prop="valuetype">
              <el-select v-model="formulaForm.valuetype" placeholder="请选择值类型" disabled>
                <el-option
                  v-for="dict in dict.type.sys_value_type"
                  :key="dict.value"
                  :label="dict.label"
                  :value="parseInt(dict.value)"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="6">
            <el-form-item label="当前值" prop="tagvalue">
              <el-input v-model="formulaForm.tagvalue" placeholder="无需输入" disabled/>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="单位" prop="units">
              <el-input v-model="formulaForm.units" placeholder="请输入单位，如°C，A，V，HZ" disabled/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="计算公式" prop="tagformula">
              <el-input v-model="formulaForm.tagformula" type="textarea" placeholder="请输入内容" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFormulaForm">确 定</el-button>
        <el-button @click="formulaCancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listQstag, getQstag, delQstag, addQstag, updateQstag,alarmLevelSelect, getDbName } from "@/api/system/qstag";
import {getToken} from "@/utils/auth";

export default {
  name: "Qstag",
  dicts: ['sys_alarm_forbid', 'sys_control_permissions', 'sys_read_type', 'sys_data_save_time', 'sys_is_alarm', 'sys_value_type','sys_is_custom_type'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 变量寄存器配置表格数据
      qstagList: [],
      //报警等级
      alarmLevelOptions: undefined,
      // 弹出层标题
      title: "",
      // 编辑公式弹出层标题
      formulaTitle: "",
      // 是否显示弹出层
      open: false,
      // 是否显示编辑公式弹出层
      formulaOpen: false,
      // 导入参数
      upload: {
        // 是否显示弹出层（用户导入）
        open: false,
        // 弹出层标题（用户导入）
        title: "",
        // 是否禁用上传
        isUploading: false,
        // 是否更新已经存在的用户数据
        updateSupport: 0,
        dbName : getDbName(),
        // 设置上传的请求头部
        headers: { Authorization: "Bearer " + getToken() },
        // 上传的地址
        url: process.env.VUE_APP_BASE_API + "/system/qstag/importData"
      },
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        tagname: null,
        tagnameCN: null,
        valuetype: null,
        boolType: null,
        itemdrid: null,
        itemid: null,
        itemreadtype: null,
        itemxs: null,
        savetime: null,
        units: null,
        tagdesc: null,
        alarmtag: null,
        alarmforbid: null,
        alarmgroup: null,
        alarmlevel: null,
        alarmdelay: null,
        hhValue: null,
        hhAlarmLevel: null,
        hvalue: null,
        halarmLevel: null,
        lvalue: null,
        lalarmLevel: null,
        llValue: null,
        llAlarmLevel: null,
        control: null,
      },
      // 表单参数
      form: {},
      // 编辑公式的表单参数
      formulaForm: {},
      // 列信息
      columns: [
        { key: 0, label: `ID`, visible: true },
        { key: 1, label: `寄存器名称`, visible: true },
        { key: 2, label: `寄存器中文含义`, visible: true },
        { key: 3, label: `寄存器地址`, visible: true },
        { key: 4, label: `寄存器值`, visible: true },
        { key: 5, label: `单位`, visible: true },
        { key: 6, label: `描述`, visible: true },
        { key: 7, label: `值类型`, visible: true },
        { key: 8, label: `通讯驱动`, visible: true },
        { key: 9, label: `采集频率`, visible: true },
        { key: 10, label: `读写`, visible: true },
        { key: 11, label: `权限`, visible: true },
        { key: 12, label: `系数`, visible: false },
        { key: 13, label: `更新时间`, visible: true },
        { key: 14, label: `是否报警`, visible: false },
        { key: 15, label: `是否禁止`, visible: false },
        { key: 16, label: `报警分组`, visible: false },
        { key: 17, label: `报警等级`, visible: false },
        { key: 18, label: `报警延时`, visible: false },
        { key: 19, label: `高高限值`, visible: false },
        { key: 20, label: `高限值`, visible: false },
        { key: 21, label: `低限值`, visible: false },
        { key: 22, label: `低低限值`, visible: false },
        { key: 23, label: `高高限报警等级`, visible: false },
        { key: 24, label: `高限报警等级`, visible: false },
        { key: 25, label: `低限报警等级`, visible: false },
        { key: 26, label: `低低限报警等级`, visible: false },
      ],
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
    this.getAlarmLevelSelect();
  },
  methods: {
    /** 查询变量寄存器配置列表 */
    getList() {
      this.loading = true;
      listQstag(this.queryParams).then(response => {
        this.qstagList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 查询报警等级类型 */
    getAlarmLevelSelect() {
      alarmLevelSelect().then(response => {
        this.alarmLevelOptions = response.data;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 取消编辑公式按钮
    formulaCancel() {
      this.formulaOpen = false;
      this.formulaReset();
    },
    // 表单重置
    reset() {
      this.form = {
        tagid: null,
        tagname: null,
        tagnameCN: null,
        drTypeCode: null,
        tagvalue: null,
        valuetype: null,
        boolType: null,
        itemdrid: null,
        itemtype: null,
        itemid: null,
        itemdradd: null,
        itemreadtype: null,
        itemxs: null,
        savetime: null,
        backtime: null,
        updatetime: null,
        units: null,
        tagdesc: null,
        tagvartype: null,
        inivalue: null,
        alarmnote: null,
        alarmtag: null,
        alarmforbid: null,
        alarmgroup: null,
        alarmtype: null,
        alarmlevel: null,
        alarmdelay: null,
        alacondition: null,
        hvalue: null,
        hhValue: null,
        lvalue: null,
        llValue: null,
        tagformula: null,
        control: null,
        meteringGroup: null
      };
      this.resetForm("form");
    },
    // 编辑公式表单重置
    formulaReset() {
      this.formulaForm = {
        tagid: null,
        tagname: null,
        tagnameCN: null,
        tagvalue: null,
        valuetype: null,
        itemid: null,
        units: null,
        tagformula: null
      };
      this.resetForm("formulaForm");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.tagid)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加变量寄存器配置";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const tagId = row.tagid || this.ids
      getQstag(tagId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改变量寄存器配置";
      });
    },
    /** 修改公式操作 */
    handleFormula(row) {
      this.formulaReset();
      const tagId = row.tagid || this.ids
      getQstag(tagId).then(response => {
        this.formulaForm = response.data;
        this.formulaOpen = true;
        this.formulaTitle = "编辑变量寄存器的公式";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.tagid != null) {
            updateQstag(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addQstag(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 编辑公式提交按钮 */
    submitFormulaForm() {
      this.$refs["formulaForm"].validate(valid => {
        if (valid) {
          if (this.formulaForm.tagid != null) {
            //只更新公式，其它值不要更新
            const tagId = this.formulaForm.tagid;
            const tagFormula = this.formulaForm.tagformula;
            this.formulaForm.clean();
            this.formulaForm.tagid = tagId;
            this.formulaForm.tagformula = tagFormula;
            updateQstag(this.formulaForm).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.formulaOpen = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const tagids = row.tagid || this.ids;
      this.$modal.confirm('是否确认删除变量寄存器配置编号为"' + tagids + '"的数据项？').then(function() {
        return delQstag(tagids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/qstag/export', {
        ...this.queryParams
      }, `项目点表配置_${new Date().getTime()}.xlsx`)
    },
    /** 导入按钮操作 */
    handleImport() {
      this.upload.title = "项目点表数据导入";
      this.upload.open = true;
    },
    /** 下载模板操作 */
    importTemplate() {
      this.download('system/qstag/importTemplate', {
      }, `qstag_template_${new Date().getTime()}.xlsx`)
    },
    // 文件上传中处理
    handleFileUploadProgress(event, file, fileList) {
      this.upload.isUploading = true;
    },
    // 文件上传成功处理
    handleFileSuccess(response, file, fileList) {
      this.upload.open = false;
      this.upload.isUploading = false;
      this.$refs.upload.clearFiles();
      this.$alert("<div style='overflow: auto;overflow-x: hidden;max-height: 70vh;width:600px;padding: 10px 20px 0;'>" + response.msg + "</div>", "导入结果", { dangerouslyUseHTMLString: true,customClass: 'el-message-box' });
      this.getList();
    },
    // 提交上传文件
    submitFileForm() {
      this.$refs.upload.submit();
    }
  }
};
</script>
<style type="text/css">
.el-message-box {
  width: 600px; /* 设置弹框宽度为视口宽度的一半 */
}
</style>
